package network.server;

import java.io.IOException;
import java.util.Vector;

public class ChatRoom {
	
	private static int i = 1;
	
	Vector threadsInThisRoom = new Vector();
	String title;
	Integer roomNo = i++;
	
	public void insertToRoom(ChatThread t) {
		synchronized (threadsInThisRoom) {
			threadsInThisRoom.add(t);
			sendToAll(t.nickName + "님이 입장하셨습니다.");
		}
	}
	
	public void removeFromRoom(ChatThread t) {
		synchronized (threadsInThisRoom) {
			threadsInThisRoom.remove(t);
			sendToAll(t.nickName + "님이 퇴장하셨습니다.");
		}
	}
	
	public void sendToAll(String message) {
		synchronized (threadsInThisRoom) {
			int size = threadsInThisRoom.size();
			for (int i = 0; i < size; i++) {
				ChatThread t = (ChatThread) threadsInThisRoom
												.get(i);
				try {
					t.send(message);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}
